from typing import *


class Solution:

    def minimumIndex(self, nums: List[int]) -> int:
        n = len(nums)
        if n == 1: return -1
        f = [0] * n
        z = nums[n - 1]
        h = DefaultDict(int)
        h[nums[n - 1]] += 1
        for i in range(n - 2, -1, -1):
            f[i] = z
            h[nums[i]] += 1
            if h[nums[i]] * 2 > n - i:
                z = nums[i]
            if h[z] * 2 <= n - i:
                z = 0
        z = 0
        h = DefaultDict(int)
        for i in range(n - 1):
            h[nums[i]] += 1
            if h[nums[i]] * 2 > i + 1:
                z = nums[i]
            if h[z] * 2 <= i + 1:
                z = 0
            if z != 0 and z == f[i]:
                return i
        return -1